<template>
  <div class="container">
    <div class="div_block">
      <div class="form_block">
        <el-form>
          <el-form-item>
            <el-input placeholder="用户名" v-model="form.username"></el-input>
          </el-form-item>
          <el-form-item>
            <el-input placeholder="登陆密码" v-model="form.password" type="password"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div class="btn_block">
        <el-button type="success" plain round @click="do_login" style="width: 100%;margin-bottom: 30px">登录</el-button>

        <div style="text-align: right">
          您还没有账号？ <a @click="to_register" style="color: lightblue">马上注册</a>
        </div>

      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "LoginBlock",
  data(){
    return {
      form:{
        username:"",
        password:""
      },
      token:"TestToken",
    }
  },
  methods:{
    to_register(){
      this.$router.push('/sign/register');
    },
    async do_login(){

      this.form.password = this.$md5(this.form.password);
      console.log(this.form)
      const{data:res} = await this.$http.post("http://120.46.132.13:8081/login",this.form)

      console.log(res)
      if(res.code===0){
        window.sessionStorage.setItem("pass",this.form.password)
        this.$notify.success(res.message);
        // 后端返回用户信息，包含是否是管理员，以及token
        this.$store.commit('saveIsAdmin',res.data.isAdmin)
        this.token = res.data.token;
        this.$store.commit('saveToken',this.token)
        sessionStorage.setItem("token",this.token)
        sessionStorage.setItem("userId",res.data.user.userId)
        //获取token方法
        //console.log(this.$store.state.token)
        this.$router.push('/home');
      }else{
        this.$notify.error(res.message);
      }

    }
  }
}
</script>

<style scoped>
  .container{
    height: 100%;
    width: 100%;
  }

  .div_block{
    width: 80%;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
  }

  .form_block{

  }
  .btn_block{
    margin-top: 25px;
    color: #ffffff;
  }

</style>
